home *** CD-ROM | disk | FTP | other *** search
- unit Strm3u;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, ExtCtrls, Buttons;
-
- type
- TPointList = class(TComponent)
- protected
- {$ifdef Windows}
- procedure WriteComponents(Writer: TWriter); override;
- {$else}
- procedure GetChildren(Proc: TGetChildProc); override;
- {$endif}
- end;
-
- TPointData = class(TComponent)
- private
- FX, FY: Word;
- {$ifdef Windows}
- protected
- function HasParent: Boolean; override;
- {$endif}
- public
- constructor CreateXY(AOwner: TComponent; AX, AY: Word);
- procedure SwapXY;
- published
- property X: Word read FX write FX default 0;
- property Y: Word read FY write FY default 0;
- end;
-
- TForm1 = class(TForm)
- PaintBox1: TPaintBox;
- Bevel1: TBevel;
- MakeBtn: TButton;
- SaveBtn: TButton;
- LoadBtn: TButton;
- SwapBtn: TButton;
- procedure FormCreate(Sender: TObject);
- procedure PaintBox1Paint(Sender: TObject);
- procedure MakeBtnClick(Sender: TObject);
- procedure SaveBtnClick(Sender: TObject);
- procedure LoadBtnClick(Sender: TObject);
- procedure SwapBtnClick(Sender: TObject);
- private
- PointList: TPointList;
- procedure ClearPoints;
- end;
-
- var
- Form1: TForm1;
- Pt: TPointData;
- Loop: Integer;
-
- const
- DataFile = 'POINTS3.DAT';
-
- implementation
-
- {$R *.DFM}
-
- {$ifdef Windows}
- procedure TPointList.WriteComponents(Writer: TWriter);
- var
- Loop: Integer;
- begin
- { inherited version does nothing - no need to call it }
- for Loop := 0 to ComponentCount - 1 do
- Writer.WriteComponent(Components[Loop]);
- end;
- {$else}
- procedure TPointList.GetChildren(Proc: TGetChildProc);
- var
- Loop: Integer;
- begin
- { inherited version does nothing - no need to call it }
- for Loop := 0 to ComponentCount - 1 do
- Proc(Components[Loop]);
- end;
- {$endif}
-
- constructor TPointData.CreateXY(AOwner: TComponent; AX, AY: Word);
- begin
- inherited Create(AOwner);
- FX := AX;
- FY := AY;
- end;
-
- {$ifdef Windows}
- function TPointData.HasParent: Boolean;
- begin
- Result := True;
- end;
- {$endif}
-
- procedure TPointData.SwapXY;
- begin
- Tag := FX;
- FX := FY;
- FY := Tag;
- end;
-
- procedure TForm1.ClearPoints;
- begin
- PointList.DestroyComponents
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- PointList := TPointList.Create(Self);
- end;
-
- procedure TForm1.PaintBox1Paint(Sender: TObject);
- begin
- for Loop := 0 to PointList.ComponentCount - 1 do
- begin
- Pt := PointList.Components[Loop] as TPointData;
- if Loop = 0 then
- PaintBox1.Canvas.MoveTo(Pt.X, Pt.Y)
- else
- PaintBox1.Canvas.LineTo(Pt.X, Pt.Y)
- end;
- end;
-
- procedure TForm1.MakeBtnClick(Sender: TObject);
- begin
- ClearPoints;
- for Loop := 1 to Random(40) + 1 do
- begin
- Pt := TPointData.CreateXY(PointList,
- Random(PaintBox1.Width),
- Random(PaintBox1.Height));
- PaintBox1.Invalidate;
- end;
- end;
-
- procedure TForm1.SaveBtnClick(Sender: TObject);
- var
- Stream: TFileStream;
- begin
- Stream := TFileStream.Create(DataFile, fmCreate);
- try
- Stream.WriteComponent(PointList);
- finally
- Stream.Free;
- end;
- ClearPoints;
- PaintBox1.Invalidate;
- end;
-
- procedure TForm1.LoadBtnClick(Sender: TObject);
- var
- Stream: TFileStream;
- begin
- ClearPoints;
- Stream := TFileStream.Create(DataFile, fmOpenRead or fmShareDenyWrite);
- try
- Stream.ReadComponent(PointList);
- finally
- Stream.Free;
- end;
- PaintBox1.Invalidate;
- end;
-
- procedure TForm1.SwapBtnClick(Sender: TObject);
- begin
- for Loop := 0 to PointList.ComponentCount - 1 do
- (PointList.Components[Loop] as TPointData).SwapXY;
- PaintBox1.Invalidate;
- end;
-
- initialization
- Randomize;
- RegisterClass(TPointData);
- end.
-